<template>
  <div class="main">
    <div class="title"><span class="span">丨</span>标签管理</div><hr>
    <div class="header">
      <el-button class="button" @click="drawer=true">新增</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" stripe height="100%">
        <el-table-column prop="imgURL" label="标签" header-align="center" align="center">
          <template #default="{row}">
            <img :src="row.imgURL" style="width: 50%;">
          </template>
        </el-table-column>
        <el-table-column prop="size" label="尺寸" header-align="center" align="center"></el-table-column>
        <el-table-column prop="time" label="创建时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="" label="操作" header-align="center" align="center">
          <template #default="scope">
            <el-button link type="primary"  @click="deleteData(scope.$index)">
            删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-drawer
    v-model="drawer"
    title="新增标签"
  >
  <el-upload
    class="avatar-uploader"
    action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
  <template #footer>
      <div>
        <el-button type="primary" @click="addData" class="button">增加</el-button>
        <el-button @click="drawer=false" class="button">取消</el-button>
      </div>
    </template>
  </el-drawer>

  </div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'

const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  console.log(imageUrl.value);
  
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  // if (rawFile.type !== 'image/jpeg') {
  //   ElMessage.error('Avatar picture must be JPG format!')
  //   return false
  // } else if (rawFile.size / 1024 / 1024 > 2) {
  //   ElMessage.error('Avatar picture size can not exceed 2MB!')
  //   return false
  // }
  return true
}


import {ref,reactive} from 'vue'
const tableData = reactive([{
  size:'100*100',
  time:'2024.11.11',
  imgURL:'../../../../label1.png'
},
{
  size:'100*100',
  time:'2024.11.11',
  imgURL:'../../../../label2.png'
},
{
  size:'100*100',
  time:'2024.11.11',
  imgURL:'../../../../label3.png'
},
{
  size:'100*100',
  time:'2024.11.11',
  imgURL:'../../../../label4.png'
},
{
  size:'100*100',
  time:'2024.11.11',
  imgURL:'../../../../label5.png'
}
])
const drawer = ref(false)
const insertData = reactive({
  name:'',
  batch:'',
  type:'',
  introduce:'',
  time:''
})



const deleteData=(index)=>{
  tableData.splice(index, 1)
}

const addData = () =>{
  const today = new Date();
  const year = today.getFullYear();  
  const month = today.getMonth() + 1; 
  const day = today.getDate();       
  insertData.time=year+'.'+month+'.'+day
  tableData.push({...insertData})
  for(const key in insertData){
    insertData[key]=''
  }
  drawer.value=false
}
</script>

<style scoped>
@import url('./Label.css');
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>